<template>
    <div class="register">
        <div class="e-mail">
            <span><img src="@/assets/account.png" /></span><input type="text" v-model="username" placeholder="请输入用户名" />
        </div>
        <div class="e-mail">
            <span><img src="@/assets/email.png" /></span><input type="text" v-model="email" placeholder="请输入邮箱" />
        </div>
        <div class="password">
            <span><img src="@/assets/password.png" /></span><input type="password" v-model="pwd" placeholder="请输入密码" />
        </div>

        <div class="authcode">
            <span><img src="@/assets/authcode.png" /></span><input type="text" v-model="code" placeholder="验证码" />
            <span class="code" @click="send_code">获取验证码</span>
        </div>
        <button @click="register">注册</button>
    </div>
</template>

<script setup>
import { ref } from "vue";
import { sendCode, myRegister } from "../../request/index.js";
import { useRouter } from "vue-router";
import { showToast } from "vant";
let moment = require("moment");
let router = useRouter();
let username = ref("");
let pwd = ref("");
let email = ref("");
let code = ref("");
let send_code = () => {
    let em = email.value;
    sendCode({ email: em })
        .then((data) => {
            showToast(data.msg);
        })
        .catch((err) => {
            console.log(err);
            showToast(err.msg);
        });
};
let register = () => {
    let user_acc = email.value;
    let user_pwd = pwd.value;
    let user_nick_name = username.value;
    let now = moment().format("YYYY-MM-DD hh:mm:ss");
    let email_code = code.value;
    // console.log(now);
    myRegister({ user_acc, user_pwd, user_nick_name, user_create_time: now, email_code })
        .then((res) => {
            showToast(res.msg);
            if (res.code == 200) {
                router.push({ path: "/loginregister/login" });
            }
        })
        .catch((err) => {
            console.log(err);
        });
    // console.log(username.value, pwd.value, email.value, code.value);
};
</script>

<style lang="scss" scoped>
.register {
    .code {
        color: #828282;
        border: 1px solid #ccc;
        box-sizing: border-box;
        padding: 0.1875rem;
        height: 1.875rem;
        display: flex;
        flex: 1;
        font-size: 13px;
        border-radius: 1rem;
        justify-content: center;
        align-items: center;
    }
    .password {
        input {
            width: 100%;
        }
    }
    div {
        width: 100%;
        height: 3.125rem;
        border-bottom: 1px solid #dedede;
        display: flex;
        align-items: center;
        span {
            img {
                height: 1.5625rem;
            }
        }
    }
    input {
        font-size: 15px;
        height: 2.5rem;
        padding-left: 0.625rem;
        color: #828282;
    }
    input::placeholder {
        color: #b5b5b5;
    }
    button {
        font-size: 1rem;
        margin-top: 1.25rem;
        color: #fff;
        background-color: #ff9092;
        width: 100%;
        height: 2.5rem;
        border-radius: 1rem;
    }
}
</style>
